package com.hhit.bigscreenbackmanage.service.impl.jjgl;

import com.alibaba.fastjson.JSONObject;
import com.hhit.bigscreenbackmanage.entity.jjgl.TJjglYszxqk;
import com.hhit.bigscreenbackmanage.dao.jjgl.TJjglYszxqkDao;
import com.hhit.bigscreenbackmanage.service.jjgl.TJjglYszxqkService;
import com.hhit.bigscreenbackmanage.util.PageUtil;
import com.hhit.bigscreenbackmanage.vo.jjgl.SgtPsDpListVO;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 各单位预算执行情况(TJjglYszxqk)表服务实现类
 *
 * @author makejava
 * @since 2021-07-22 16:02:23
 */
@Service("tJjglYszxqkService")
public class TJjglYszxqkServiceImpl implements TJjglYszxqkService {

    private static String[] cityArr = {"建设分公司", "南京", "苏州", "无锡", "徐州", "常州", "镇江", "扬州", "泰州", "南通", "盐城", "淮安", "宿迁", "连云港"};

    @Resource
    private TJjglYszxqkDao tJjglYszxqkDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public TJjglYszxqk queryById(String id) {
        return this.tJjglYszxqkDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @return 对象列表
     */
    @Override
    public Map<String, Object> queryAllByLimit(JSONObject jsonObject) {
        Map<String, Object> out = new HashMap<>();
        PageUtil page = new PageUtil();
        String pageSize = jsonObject.get("pageSize").toString();
        String pageNo = jsonObject.get("pageNo").toString();
        int count = tJjglYszxqkDao.queryAllCount(jsonObject);
        page.pageCount = Integer.parseInt(pageSize);// 每页数
        page.init(count, Integer.parseInt(pageNo));//参数1：总行数，参数2：当前页
        jsonObject.put("rowNum", page.getRowNum());
        jsonObject.put("pageCount", page.getPageCount());
        List<TJjglYszxqk> list = tJjglYszxqkDao.queryAll(jsonObject);
        out.put("page", page);
        out.put("list", list);
        return out;
    }

    /**
     * 新增数据
     *
     * @param tJjglYszxqk 实例对象
     * @return 实例对象
     */
    @Override
    public TJjglYszxqk insert(TJjglYszxqk tJjglYszxqk) {
        this.tJjglYszxqkDao.insert(tJjglYszxqk);
        return tJjglYszxqk;
    }

    /**
     * 修改数据
     *
     * @param tJjglYszxqk 实例对象
     * @return 实例对象
     */
    @Override
    public TJjglYszxqk update(TJjglYszxqk tJjglYszxqk) {
        this.tJjglYszxqkDao.update(tJjglYszxqk);
        return this.queryById(tJjglYszxqk.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.tJjglYszxqkDao.deleteById(id) > 0;
    }

    @Override
    public Map<String, Object> getYszxqk(JSONObject obj) {
        Map<String, Object> out = new HashMap<>();
        String[] ydfs = {"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"};
        String[] ljwcbl = {"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"};
        // 每个月完成量
        TJjglYszxqk yszxqk = tJjglYszxqkDao.getYszxqk();
        if (StringUtils.isEmpty(yszxqk.getYi()) || yszxqk.getYi().indexOf("-") == 0 || "0.0".equals(yszxqk.getYi())) {
            ydfs[0] = "";
        } else {
            ydfs[0] = fourNum(yszxqk.getYi());
        }
        if (StringUtils.isEmpty(yszxqk.getEr()) || yszxqk.getEr().indexOf("-") == 0 || "0.0".equals(yszxqk.getEr())) {
            ydfs[1] = "";
        } else {
            ydfs[1] = fourNum(yszxqk.getEr());
        }
        if (StringUtils.isEmpty(yszxqk.getSan()) || yszxqk.getSan().indexOf("-") == 0 || "0.0".equals(yszxqk.getSan())) {
            ydfs[2] = "";
        } else {
            ydfs[2] = fourNum(yszxqk.getSan());
        }
        if (StringUtils.isEmpty(yszxqk.getSi()) || yszxqk.getSi().indexOf("-") == 0 || "0.0".equals(yszxqk.getSi())) {
            ydfs[3] = "";
        } else {
            ydfs[3] = fourNum(yszxqk.getSi());
        }
        if (StringUtils.isEmpty(yszxqk.getWu()) || yszxqk.getWu().indexOf("-") == 0 || "0.0".equals(yszxqk.getWu())) {
            ydfs[4] = "";
        } else {
            ydfs[4] = fourNum(yszxqk.getWu());
        }
        if (StringUtils.isEmpty(yszxqk.getLiu()) || yszxqk.getLiu().indexOf("-") == 0 || "0.0".equals(yszxqk.getLiu())) {
            ydfs[5] = "";
        } else {
            ydfs[5] = fourNum(yszxqk.getLiu());
        }
        if (StringUtils.isEmpty(yszxqk.getQi()) || yszxqk.getQi().indexOf("-") == 0 || "0.0".equals(yszxqk.getQi())) {
            ydfs[6] = "";
        } else {
            ydfs[6] = fourNum(yszxqk.getQi());
        }
        if (StringUtils.isEmpty(yszxqk.getBa()) || yszxqk.getBa().indexOf("-") == 0 || "0.0".equals(yszxqk.getBa())) {
            ydfs[7] = "";
        } else {
            ydfs[7] = fourNum(yszxqk.getBa());
        }
        if (StringUtils.isEmpty(yszxqk.getQiu()) || yszxqk.getQiu().indexOf("-") == 0 || "0.0".equals(yszxqk.getQiu())) {
            ydfs[8] = "";
        } else {
            ydfs[8] = fourNum(yszxqk.getQiu());
        }
        if (StringUtils.isEmpty(yszxqk.getShi()) || yszxqk.getShi().indexOf("-") == 0 || "0.0".equals(yszxqk.getShi())) {
            ydfs[9] = "";
        } else {
            ydfs[9] = fourNum(yszxqk.getShi());
        }
        if (StringUtils.isEmpty(yszxqk.getShiyi()) || yszxqk.getShiyi().indexOf("-") == 0 || "0.0".equals(yszxqk.getShiyi())) {
            ydfs[10] = "";
        } else {
            ydfs[10] = fourNum(yszxqk.getShiyi());
        }
        if (StringUtils.isEmpty(yszxqk.getShier()) || yszxqk.getShier().indexOf("-") == 0 || "0.0".equals(yszxqk.getShier())) {
            ydfs[11] = "";
        } else {
            ydfs[11] = fourNum(yszxqk.getShier());
        }

        // 每个月累计完成量
        TJjglYszxqk yszxqkLj = tJjglYszxqkDao.getYszxqkLj();
        // 总量
        String all = yszxqkLj.getDzxjj();
        ljwcbl[0] = chenFa(chuFa(yszxqkLj.getYi(), all), 100);
        ljwcbl[1] = chenFa(chuFa(yszxqkLj.getEr(), all), 100);
        ljwcbl[2] = chenFa(chuFa(yszxqkLj.getSan(), all), 100);
        ljwcbl[3] = chenFa(chuFa(yszxqkLj.getSi(), all), 100);
        ljwcbl[4] = chenFa(chuFa(yszxqkLj.getWu(), all), 100);
        ljwcbl[5] = chenFa(chuFa(yszxqkLj.getLiu(), all), 100);
        ljwcbl[6] = chenFa(chuFa(yszxqkLj.getQi(), all), 100);
        ljwcbl[7] = chenFa(chuFa(yszxqkLj.getBa(), all), 100);
        ljwcbl[8] = chenFa(chuFa(yszxqkLj.getQiu(), all), 100);
        ljwcbl[9] = chenFa(chuFa(yszxqkLj.getShi(), all), 100);
        ljwcbl[10] = chenFa(chuFa(yszxqkLj.getShiyi(), all), 100);
        ljwcbl[11] = chenFa(chuFa(yszxqkLj.getShier(), all), 100);
        out.put("ydfs", ydfs);
        for (int i = 0; i < ljwcbl.length; i++) {
            if ("0.00".equals(ljwcbl[i])) {
                ljwcbl[i] = "";
            }
        }
        out.put("ljwcbl", ljwcbl);
        return out;
    }

    @Override
    public Map<String, Object> getJjXmYsZx(JSONObject obj) {
        Map<String, Object> outBean = new HashMap<>();
        // 每个月累计完成量
        TJjglYszxqk yszxqkLj = tJjglYszxqkDao.getYszxqkLj();
        outBean.put("out", fourNum2(yszxqkLj.getDzxjj()));
        Calendar cal = Calendar.getInstance();
        //加1表示当前月，不加表示上个月
        int month = cal.get(Calendar.MONTH);
        switch (month) {
            case 1:
                outBean.put("by", fourNum2(yszxqkLj.getYi()));
                break;
            case 2:
                outBean.put("by", fourNum2(yszxqkLj.getEr()));
                break;
            case 3:
                outBean.put("by", fourNum2(yszxqkLj.getSan()));
                break;
            case 4:
                outBean.put("by", fourNum2(yszxqkLj.getSi()));
                break;
            case 5:
                outBean.put("by", fourNum2(yszxqkLj.getWu()));
                break;
            case 6:
                outBean.put("by", fourNum2(yszxqkLj.getLiu()));
                break;
            case 7:
                outBean.put("by", fourNum2(yszxqkLj.getQi()));
                break;
            case 8:
                outBean.put("by", fourNum2(yszxqkLj.getBa()));
                break;
            case 9:
                outBean.put("by", fourNum2(yszxqkLj.getQiu()));
                break;
            case 10:
                outBean.put("by", fourNum2(yszxqkLj.getShi()));
                break;
            case 11:
                outBean.put("by", fourNum2(yszxqkLj.getShiyi()));
                break;
            case 12:
                outBean.put("by", fourNum2(yszxqkLj.getShier()));
                break;
        }
        outBean.put("wcl", fourNum2(yszxqkLj.getWcbl()));
        return outBean;
    }

    @Override
    public Map<String, Object> getJjXmYsZxBar(JSONObject obj) throws Exception {
        Map<String, Object> outBean = new HashMap<>();
        List<Map<String, Object>> outList = new ArrayList<>();
        List<Map<String, Object>> list = tJjglYszxqkDao.getJjxmysBarData();
        for (int i = 0; i < cityArr.length; i++) {
            String city = cityArr[i];
            for (Map map : list) {
                if (map.get("name").toString().contains(city)) {
                    map.put("name", city);
                    outList.add(map);
                    break;
                } else {
                    continue;
                }
            }
        }
        outBean.put("barData", outList);
        return outBean;
    }

    @Override
    public Map<String, Object> getYysZxDpList(JSONObject obj) {
        Map<String, Object> outBean = new HashMap<>();
        List<TJjglYszxqk> yysZxDpList = tJjglYszxqkDao.getYysZxDpList();
        List<TJjglYszxqk> collect = yysZxDpList.stream().filter(item
                -> !"合计".equals(item.getJgdw())).collect(Collectors.toList());
        outBean.put("list", collect);
        return outBean;
    }

    /**
     * 除法
     *
     * @param a
     * @param b
     * @return
     */
    public static String chuFa(String a, String b) {
        if (StringUtils.isEmpty(a)) {
            a = "0";
        }
        if (StringUtils.isEmpty(b)) {
            b = "0";
        }
        float af = Float.parseFloat(a);
        float bf = Float.parseFloat(b);
        //“0.0000”确定精度
        DecimalFormat dF = new DecimalFormat("0.0000");
        return dF.format(af / bf);
    }

    /**
     * 乘法
     *
     * @param a
     * @param b
     * @return
     */
    public static String chenFa(String a, int b) {
        if (StringUtils.isEmpty(a)) {
            a = "0";
        }
        float af = Float.parseFloat(a);
        float result = af * b;
        DecimalFormat dF = new DecimalFormat("0.00");
        return dF.format(result);
    }

    /**
     * @param a
     * @return
     */
    public static String fourNum(String a) {
        if (StringUtils.isEmpty(a)) {
            return "0";
        }
        float af = Float.parseFloat(a);
        //“0.0000”确定精度
        DecimalFormat dF = new DecimalFormat("0.0000");
        return dF.format(af);
    }

    /**
     * @param a
     * @return
     */
    public static String fourNum2(String a) {
        if (StringUtils.isEmpty(a)) {
            return "0";
        }
        float af = Float.parseFloat(a);
        //“0.0000”确定精度
        DecimalFormat dF = new DecimalFormat("0.00");
        return dF.format(af);
    }

}
